package com.swim.session;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.swim.entity.Collaboration;
import com.swim.entity.CollaborationRequest;
import com.swim.entity.RegisteredUser;
import com.swim.entity.Skill;

@Stateless
@RemoteBinding(jndiBinding="CollaborationRequestBeanJNDI")
public class CollaborationRequestBean implements CollaborationRequestRemote {

	@PersistenceContext(unitName = "SWIM")
	EntityManager database;

	/**
	 * @param collId the id of the request's collaboration
	 * @return the request of collaboration selected by the id
	 */
	private CollaborationRequest getCollaborationRequest(int collId) {
		try{
			Query q = database.createQuery("SELECT c FROM CollaborationRequest c WHERE c.id=:id");
			q.setParameter("id", collId);
			CollaborationRequest result =  (CollaborationRequest) q.getSingleResult();
			return result;
	   	} catch (EntityNotFoundException exc) { 
			System.err.println("UFF1");
		} catch (javax.persistence.NoResultException exc) { 
			System.err.println("UFF2");
		} catch (NonUniqueResultException exc) {
			System.err.println("UFF3");
		}
		return null;
	}
	
	/**
	 * @param from the RegisteredUser who sent the request of collaboration
	 * @param to the RegisteredUser who received the request of collaboration
	 * @param skill the ability required for this request of collaboration
	 */
	public void newCollaborationRequest(String from, String to,Skill skill) {
		String date = Util.getNow();
		RegisteredUserLocal r = getRegisteredUserLocal();
		CollaborationRequest req = new CollaborationRequest(r.getUserWithEmail(from), r.getUserWithEmail(to), skill, date);
		database.persist(req);
	}
	

	/**
	 * @return the local interface of registered user
	 */
	
	private RegisteredUserLocal getRegisteredUserLocal() {
		InitialContext jndiContext;
		Object ref = null;
		try {
			jndiContext = new InitialContext();
			ref = jndiContext.lookup("RegisteredUserBeanLocalJNDI");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (RegisteredUserLocal) ref;
	}


	/**
	 * @param emailTo the email of a person who received some request of collaboration
	 * @return the list of the collaboration's request received from the user
	 */
	public List<CollaborationRequest> getCollaborationRequests(String emailTo) {
		try {
			RegisteredUserLocal r = getRegisteredUserLocal();
    		List<CollaborationRequest> resultList = new ArrayList<CollaborationRequest>();
    		Set<CollaborationRequest> set = r.getUserWithEmail(emailTo).getReceivedCollaborationRequests();
    		resultList.addAll(set);
			return resultList;
    	} catch (EntityNotFoundException exc) { 
    		System.err.println("UFF1");
    	} catch (javax.persistence.NoResultException exc) { 
    		System.err.println("UFF2");
    	} catch (NonUniqueResultException exc) {
    		System.err.println("UFF3");
    	}
    	return null;
	}


	/**
	 * @param collId the id of the collaboration's request
	 * @param accept reports if the request is accepted 
	 */
	public void acceptCollaborationRequest(int collId, boolean accept) {
		if(accept) {
			CollaborationRequest req = getCollaborationRequest(collId);
			RegisteredUser to = req.getEmailTo();
			Collaboration c = new Collaboration(Util.getNow(), to, req.getEmailFrom(), req.getIdSkill(), null);
			database.persist(c);
			database.remove(req);
			
		} else {
			database.remove(getCollaborationRequest(collId));
		}
	}

}
